package com.sms.jiujia.service;

import com.sms.jiujia.model.MainData;
import com.sms.jiujia.ui.setting.Constant;
import com.sms.jiujia.utils.DateUtils;
import com.sms.jiujia.utils.LogUtils;
import com.sms.jiujia.utils.PushMsgUtils;
import com.sms.jiujia.utils.UtilTools;
import okhttp3.Response;

import java.text.ParseException;
import java.util.Date;
import java.util.Objects;
import java.util.TimerTask;

/**
 * @author songmingsong
 * @date 2023/5/11
 */
public class CurlTimer extends TimerTask {
    private ProcessBuilder curlCmd = null;


    public CurlTimer(ProcessBuilder curlCmd) {
        this.curlCmd = curlCmd;
    }

    @Override
    public void run() {
        LogUtils.getLayoutConsoleLogInstance().log("[info]->"+new Date() + " : 任务「" + Constant.CITY + "」被执行。");
        String execCurlJson = UtilTools.execCurl(curlCmd);
        MainData mainData = UtilTools.coverJson(execCurlJson);
        LogUtils.getLayoutConsoleLogInstance().log(mainData.toString());
        Response response = null;
        try {
            response = PushMsgUtils.pushDeer(mainData, false);
        } catch (ParseException ex) {
            throw new RuntimeException(ex);
        }
        if (Objects.isNull(response)) {
            LogUtils.getLayoutConsoleLogInstance().log("[info]->"+DateUtils.changeDate(new Date()) + "数据无变化！不推送");
        }
        if (response != null && response.isSuccessful()) {
            LogUtils.getLayoutConsoleLogInstance().log("[info]->"+DateUtils.changeDate(new Date()) + "推送成功！\n" + mainData);
        }
        if (response != null && !response.isSuccessful()) {
            LogUtils.getLayoutConsoleLogInstance().log("[info]->"+DateUtils.changeDate(new Date()) + "推送失败！请检查key或重启应用");
        }
    }


}
